<?php
	defined('IN_TS') or die('Access Denied.');
	
	$userid = aac('user')->isLogin();
	
	
	switch($ts){
		//基本信息
		case "base":
			$eventid = intval($_GET['eventid']);
			$strEvent = $new['event']->find('event',array(
				'eventid'=>$eventid,
			));
			$strEvent['content'] = tsDecode($strEvent['content']);

			
			if($strEvent['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
				tsNotice('非法操作！');
			}
			
			$title = '编辑活动信息';
			include template("edit_base");
			break;
		
		case "basedo":

			$eventid = intval($_POST['eventid']);

            $strEvent = $new['event']->find('event',array(
                'eventid'=>$eventid,
            ));


            if($strEvent['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }


			$title = trim($_POST['title']);
			$typeid = intval($_POST['typeid']);
			$starttime = trim($_POST['starttime']);
			$endtime = trim($_POST['endtime']);
			$limittime = trim($_POST['limittime']);
			$address = trim($_POST['address']);
            $coordinate = trim($_POST['coordinate']);  //坐标
			$content = tsClean($_POST['content']);

			$price = floatval(trim($_POST['price']));
			$quota = intval(trim($_POST['quota']));
			$duty = intval(trim($_POST['duty']));

			$starttime = trim(str_replace('AM','',$starttime));
			$starttime = trim(str_replace('PM','',$starttime));
			$starttime = strtotime($starttime);

			$endtime = trim(str_replace('AM','',$endtime));
			$endtime = trim(str_replace('PM','',$endtime));
			$endtime = strtotime($endtime);

			$limittime = trim(str_replace('AM','',$limittime));
			$limittime = trim(str_replace('PM','',$limittime));
			$limittime = strtotime($limittime);

			$photo = trim($_POST['photo']);

            if (intval($TS_USER['isadmin']) == 0) {
                // 过滤内容开始
                aac('system') -> antiWord($title);
                aac('system') -> antiWord($content);
                // 过滤内容结束
            }

			if($photo==''){
				tsNotice('必须上传封面照片！');
			};
			$img_path = $photo;

			// $img_path = getTextPhotos(tsDecode($content));
			// $count = count($img_path);
			// $img_path = $img_path[$count-1];
			// if($img_path==''){
			// 	tsNotice('请至少插入一张图片作为封面！');
			// };
			//$url=preg_replace('#/$#','',SITE_URL);
			//$img_path = $url.$img_path;

			//更新数据
			$new['event']->update('event',array(
				'eventid'=>$eventid,
			),array(
				'typeid' => $typeid,
				'title'	=> $title,
				'starttime'	=> $starttime,
				'limittime' => $limittime,
				'endtime'	=> $endtime,
				'address'	=> $address,
                'coordinate'	=> $coordinate,
				'content'	=> $content,
                'price'=>$price,
				'quota'=>$quota,
				'duty'=>$duty,
				'path'=> dirname($img_path),
				'photo'=> $img_path,
			));

			//上传
			// $arrUpload = tsUploadPhotoUrl($img_path,$eventid,'event',array('jpg','gif','png'));
			// if($arrUpload){
			// 	$new['event']->update('event',array(
			// 		'eventid'=>$eventid,
			// 	),array(
			// 		'path'=>$arrUpload['path'],
			// 		'photo'=>$arrUpload['url'],
			// 	));
            //     //删除缓存图片
			// 	tsDimg ( $arrUpload ['url'], 'event', '100', '', $arrUpload ['path'] );
			// 	tsDimg ( $arrUpload ['url'], 'event', '200', '', $arrUpload ['path'] );
            //     tsDimg ( $arrUpload ['url'], 'event', '300', '', $arrUpload ['path'] );
            //     tsDimg ( $arrUpload ['url'], 'event', '500', '', $arrUpload ['path'] );
			// }
			
			header("Location: ".tsUrl('event','show',array('id'=>$eventid)));
		
			break;
			
		case "join":
			
			$eventid = intval($_GET['eventid']);
			$strEvent = $new['event']->find('event',array(
				'eventid'=>$eventid,
			));

            if($strEvent['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }
			
			$arrUser = $new['event']->findAll('event_users',array(
				'eventid'=>$eventid,
			),'addtime desc');
			foreach($arrUser as $k=>$v){
				$arrUser[$k]['group']=aac('group')->getOneGroup($v['groupid']);
				$org = aac('user')->find('user_org',array('orgid'=>$v['orgid']));
				$arrUser[$k]['org']=$org;
			}
			
			
			$title = '报名信息';
			include template('edit_join');
			
			break;

	}